/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-05-30     Dell       the first version
 */




#include "oled.h"
#include "oledfont.h"


struct rt_i2c_bus_device * oled_device;


/**
 * 初始化oled设备
 * @return
 */
rt_err_t oled_init()
{
    rt_device_t device = rt_device_find(OLED_I2C_NAME);
    if(device == RT_NULL){
        rt_kprintf("can not find %s\n", OLED_I2C_NAME);
        return RT_ERROR;
    }
    oled_device = (struct rt_i2c_bus_device *)device;

//    rt_pin_mode(OLED_GND, PIN_MODE_OUTPUT_OD);
//    rt_pin_mode(OLED_VCC, PIN_MODE_OUTPUT);
//    rt_pin_write(OLED_GND, PIN_LOW);
//    rt_pin_write(OLED_VCC, PIN_HIGH);

    oled_write_cmd(0xAE);    //关闭显示

    oled_write_cmd(0xD5);    //设置显示时钟分频比/振荡器频率
    oled_write_cmd(0x80);

    oled_write_cmd(0xA8);    //设置多路复用率
    oled_write_cmd(0x3F);

    oled_write_cmd(0xD3);    //设置显示偏移
    oled_write_cmd(0x00);

    oled_write_cmd(0x40);    //设置显示开始行

    oled_write_cmd(0xA1);    //设置左右方向，0xA1正常 0xA0左右反置

    oled_write_cmd(0xC8);    //设置上下方向，0xC8正常 0xC0上下反置

    oled_write_cmd(0xDA);    //设置COM引脚硬件配置
    oled_write_cmd(0x12);

    oled_write_cmd(0x81);    //设置对比度控制
    oled_write_cmd(0xCF);

    oled_write_cmd(0xD9);    //设置预充电周期
    oled_write_cmd(0xF1);

    oled_write_cmd(0xDB);    //设置VCOMH取消选择级别
    oled_write_cmd(0x30);

    oled_write_cmd(0xA4);    //设置整个显示打开/关闭

    oled_write_cmd(0xA6);    //设置正常/倒转显示

    oled_write_cmd(0x8D);    //设置充电泵
    oled_write_cmd(0x14);

    oled_write_cmd(0xAF);    //开启显示

    oled_clear();            //OLED清屏


    return RT_EOK;
}


/***
 * oled 清屏
 * @return
 */
rt_err_t oled_clear()
{
    uint8_t i, n;
    for(i = 0; i < 8; i++){
        oled_write_cmd(0xb0 + i);
        oled_write_cmd(0x00);
        oled_write_cmd(0x10);
        for(n = 0; n < 128; n++){
            oled_write_data(0);
        }
    }
    return RT_EOK;
}


/**
 * 显示单个字符
 * @param x
 * @param y
 * @param str
 * @return
 */
rt_err_t oled_show_char(uint8_t line, uint8_t column, char str){
    uint8_t i;
    oled_set_pos((line - 1) * 2, (column - 1) * 8);
    for(i = 0; i < 8; i++){
        oled_write_data(OLED_F8x16[str - ' '][i]);
    }
    oled_set_pos((line - 1) * 2 + 1, (column - 1) * 8);
    for(i = 0; i < 8; i++){
        oled_write_data(OLED_F8x16[str - ' '][i + 8]);
    }
    return RT_EOK;
}

/***
 * 显示字符串
 * @param  line 行位置，范围：1~4
  *@param  column 列位置，范围：1~16
  *@param  str 要显示的一个字符，范围：ASCII可见字符
 * @return
 */
rt_err_t oled_show_string(uint8_t line, uint8_t column, char* str){
    uint8_t i;
    for(i = 0; str[i] != '\0'; i++){
        oled_show_char(line, column + i, str[i]);
    }
    return RT_EOK;
}

/**
 * oled显示基本信息
 * @param line 行位置，范围：1~4
 * @param column 列位置，范围：1~16
 */
void oled_show_name(uint8_t line, uint8_t column){
    uint8_t len = sizeof(MY_NAME) / sizeof(MY_NAME[0]);
    uint8_t i, j;
    for(j = 0; j < len; j++){
        oled_set_pos((line - 1) * 2, (column - 1 + j) * 8);
        for(i = 0; i < 8; i++){
            oled_write_data(MY_NAME[j][i]);
        }
        oled_set_pos((line - 1) * 2 + 1, (column - 1 + j) * 8);
        for(i = 0; i < 8; i++){
            oled_write_data(MY_NAME[j][i + 8]);
        }
    }
}

/***
 * 发送cmd命令
 * @param cmd
 */
rt_err_t oled_write_data(rt_uint8_t data){
    struct rt_i2c_msg oled_msg;
    oled_msg.addr = OLED_ADDRESS;
    rt_uint8_t buf[] = {0x40, data};
    oled_msg.buf = buf;
    oled_msg.len = 2;
    oled_msg.flags = RT_I2C_WR;
    rt_size_t len = rt_i2c_transfer(oled_device, &oled_msg, 1);
    if(len > 0){
        return RT_EOK;
    } else {
        rt_kprintf("oled send data failed, error code is %d", len);
        return RT_ERROR;
    }
}

/***
 * 发送cmd命令
 * @param cmd
 */
rt_err_t oled_write_cmd(rt_uint8_t cmd){
    struct rt_i2c_msg oled_msg;
    rt_uint8_t buf[] = {0x0, cmd};
    oled_msg.addr = OLED_ADDRESS;
    oled_msg.buf = buf;
    oled_msg.len = 2;
    oled_msg.flags = RT_I2C_WR;
    rt_size_t len = rt_i2c_transfer(oled_device, &oled_msg, 1);
    if(len > 0){
        return RT_EOK;
    } else {
        rt_kprintf("oled send cmd failed, error code is %d", len);
        return RT_ERROR;
    }
}


/**
  * @brief  OLED设置光标位置
  * @param  Y 以左上角为原点，向下方向的坐标，范围：0~7
  * @param  X 以左上角为原点，向右方向的坐标，范围：0~127
  * @retval 无
 */
void oled_set_pos(uint8_t y, uint8_t x){
//    oled_write_cmd(0xb0 + y);
//    oled_write_cmd(((x & 0xf0) >> 4) | 0x10);
//    oled_write_cmd(x & 0x0f);

    oled_write_cmd(0xB0 | y);                    //设置Y位置
    oled_write_cmd(0x10 | ((x & 0xF0) >> 4));    //设置X位置高4位
    oled_write_cmd(0x00 | (x & 0x0F));           //设置X位置低4位
}

